﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Collections;
using System.DirectoryServices;
using System.Reflection;
using System.ComponentModel;
using System.Globalization;
using System.Diagnostics;
using Novell.Directory.Ldap;

namespace Novell.DirectoryServices.Linq
{
    /// <summary>
    /// Represents a query in Directory Services.
    /// </summary>
    /// <typeparam name="T">Type of the objects returned by this query upon execution.</typeparam>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
    public class DirectoryQuery<T> : IQueryable<T>
    {
        #region Private members

        private Expression _ex;

        #endregion

        #region Constructors

        internal DirectoryQuery(Expression ex)
        {
            _ex = ex;
        }

        #endregion

        #region Properties

        public Type ElementType
        {
            get { return typeof(T); }
        }

        public Expression Expression
        {
            get { return _ex; }
        }

        public IQueryProvider Provider
        {
            get { return new DirectoryQueryProvider(); }
        }

        #endregion

        #region Methods



        /// <summary>
        /// Executes the query and returns results.
        /// </summary>
        /// <returns>Query results represented as entity objects or projection results.</returns>
        public IEnumerator<T> GetEnumerator()
        {
            object o = new DirectoryQueryProvider().Execute(_ex);
            IEnumerable e = (IEnumerable)o;
            IEnumerator r = e.GetEnumerator();
            return (IEnumerator<T>)r;
        }

        /// <summary>
        /// Executes the query and returns results.
        /// </summary>
        /// <returns>Query results represented as entity objects or projection results.</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)new DirectoryQueryProvider().Execute(_ex)).GetEnumerator();
        }

        #endregion
        public override string ToString()
        {
            //TODO: Return the query text.
            return base.ToString();
        }
    }
   

}
